﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalesSys.Comm.Utility
{
  public  class IdBulider
    {
   
      
    }

    public class TimeNumBulider
    {
        private static readonly DateTime TimeBegin = new DateTime(2010, 1, 1);
        private string LastSpan;
        private int Sequence;
        private string Format ;
        readonly object lockObj=new object();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="format">{0}:时间秒数9位，{1}:序列号3位</param>
        public TimeNumBulider(string format = "{0}{1}")
        {
            Format = format;
        }

        public string NextNum()
        {
            lock (lockObj)
            {
                //9位，到秒
                var span = (DateTime.Now - TimeBegin).TotalSeconds.ToString("000000000");
                if (span != LastSpan)
                {
                    Sequence = 0;
                    LastSpan = span;
                }
                if (Sequence > 999)
                {
                    throw new ArgumentOutOfRangeException("Sequence", "每秒最大可生成1000个订单！");
                }
                return string.Format(Format, span, (Sequence++).ToString("000"));
            }
        }
    }
}
